package com.itheima.jobs;

import com.itheima.constant.RedisConstant;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

public class ClearImgJob {

    @Autowired
    private JedisPool jedisPool;

    public void clearImg() {
        Jedis resource = jedisPool.getResource();
        //多的那个要放到前面,根据Redis中报讯的两个集合进行差值计算,获得垃圾图片名称集合
        //sdiff是进行差值计算的方法
        Set<String> sdiff = resource.sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        if (sdiff != null){
            for (String fileName : sdiff) {
                //删除七牛云服务器上的图片
                QiniuUtils.deleteFileFromQiniu(fileName);
                //从redis集合中删除图片名称
                resource.srem(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
            }
        }
        resource.close();
    }
}
